<?php
namespace Modules\Order\Services;

use Modules\Order\Entities\Order;

class DeliveryService
{
  // 创建购物订单
  public function create(Order $order, array $delivery = null)
  {
    // 创建支付信息
    if (!empty($delivery))
      $delivery = $order->delivery()->create($delivery);

    return $delivery;
  }

  // 计算订单的费用
  public function fee(Order $order, array $goods)
  {
    $result = array(
      'price' => 0,
      'discount_price' => 0, 
      'delivery_fee' => 0, 
      'delivery_discount' => 0,
      'total' => 0,
    );

    // 遍历每一件商品
    foreach ($goods as $item) {
      // 计算价格
      if ($item['price']) {
        $result['price'] += $item['price'] * $item['quantity'];
      }
    }

    // 计算总价
    $result['total'] = $result['price'] - $result['discount_price'] + $result['delivery_fee'] - $result['delivery_discount'];

    return $result;
  }
}
